﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class ExamSearchController : ApiController
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private readonly IPR_StudyMeetingMrgSys.BLL.ExamSearch bll = new IPR_StudyMeetingMrgSys.BLL.ExamSearch();
        /// <summary>
        /// 考试信息查询（分页）
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "EmpName")
                        {
                            swhere += $" AND EUEmpName like '%{item.Value}%' ";
                        }
                    }
                }

                DataSet ds = bll.GetListByPage(swhere, null, startindex, endindex);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 获取考试列表前top条（不分页）
        /// </summary>
        /// <param name="top">为0时获取全部数据</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/GetList")]
        public IHttpActionResult GetList(int top)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                string strWhere = string.Empty;
                if (!LoginUser.IsSuperAdmin)
                {
                    strWhere = "EU.EUEmpID='" + LoginUser.EmpID + "'";
                }
                DataSet ds = bll.GetList(top, strWhere, "EU.EUID DESC");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    resultMsg.Info = "获取考试列表成功";
                    resultMsg.Data = ds.Tables[0];
                    resultMsg.Count = ds.Tables[0].Rows.Count;
                }
                else
                {
                    resultMsg.Info = "未获取到考试列表";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        
        /// <summary>
        /// 获取考试结果详情
        /// </summary>
        /// <param name="EUID">考试ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/GetExamResultDetail")]
        public IHttpActionResult GetExamResultDetail(int EUID)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataSet ds = bll.GetExamResultDetail(EUID);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = ds.Tables;
                resultMsg.Count = ds.Tables[0].Rows.Count;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 获取错题统计分页列表
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/GetQuestionErrorListPage")]
        public IHttpActionResult GetQuestionErrorListPage(int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                DataSet ds = new BLL.ExamSearch().GetQuestionErrorListPage(startindex, endindex);
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        #region 手机端使用
        /// <summary>
        /// 考试信息查询（不分页）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/SearchByPhone")]
        public IHttpActionResult SearchByPhone()
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataSet ds = bll.SearchByPhone(" EU.EUEmpID='" + LoginUser.EmpID + "' ", null);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 获取考试分数（目前仅手机端使用）----业务变更未使用先行保留
        /// </summary>
        /// <param name="EUID">考试ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamSearch/GetExamScore")]
        public IHttpActionResult GetExamScore(int EUID)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                object score = bll.GetExamScore(EUID);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = score;
                resultMsg.Count = 1;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        #endregion
    }
}
